//
//  NavgationController.m
//  ai2ban
//
//  Created by mac book pro on 2018/1/6.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "NavgationController.h"
#import "HomeController.h"
#import "ShopController.h"
#import "JiFenController.h"
#import "MeController.h"
@interface NavgationController ()<UIGestureRecognizerDelegate>

@property (nonatomic,strong)UIPanGestureRecognizer *pan;

@end

@implementation NavgationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.interactivePopGestureRecognizer.enabled = NO;//关闭系统默认的侧滑功能
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget: self.interactivePopGestureRecognizer.delegate action: @selector(handleNavigationTransition:)];
    pan.delegate = self;
    self.pan = pan;
    [self.view addGestureRecognizer: pan];
}
- (void)handleNavigationTransition:(UIPanGestureRecognizer *)pan{}
- (void)popToBack{
    [self popViewControllerAnimated:YES];
}
//UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([self.topViewController isKindOfClass:[HomeController class]] || [self.topViewController isKindOfClass:[ShopController class]] || [self.topViewController isKindOfClass:[JiFenController class]]|| [self.topViewController isKindOfClass:[MeController class]]) {
        return NO;
    }
    UIView *view = self.topViewController.view;
    CGPoint point = [touch locationInView: view];
    
    CGFloat playerMaxY = [UIScreen mainScreen].bounds.size.width / 16 * 9;
    if (point.y > playerMaxY)
    {
        return YES;
    }
    else
    {
        return NO;
    }
    
}
#define imgViewW 9
#define imgViewH 16
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        //        self.navigationBarHidden = NO;
        viewController.hidesBottomBarWhenPushed = YES;
        
        UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        setBtn.frame = CGRectMake(0, 0, 44, 40);
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nav_back"]];
        [setBtn addSubview:imgView];
        imgView.frame = CGRectMake(0, (setBtn.height-imgViewH)/2.0, imgViewW, imgViewH);
        [setBtn addTarget:self action:@selector(popToBack) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:setBtn];
        viewController.navigationItem.leftBarButtonItem = leftItem;
    }
    [super pushViewController:viewController animated:animated];
    
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
    
    return [super popViewControllerAnimated:animated];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)setPanGestureEnable:(BOOL)PanGestureEnable {
    
    _PanGestureEnable = PanGestureEnable;
    
    self.pan.enabled = PanGestureEnable;
}


@end
